// pages/device/range/feedRecord/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js")
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    submitState:false,
    qr_id:0,
    qrCode:{},
    feedType:'',
    weight:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options',options)
    if(options.qr_id){
      this.data.qr_id = parseInt(options.qr_id);
      this.onHttpInit()
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/device/range/qrCode/detail', {
      qr_code_id:that.data.qr_id,
    }, function (res) {
      console.log("传感器设备上报记录响应数据", res)
      if (res.data.code == 0) {
        that.setData({
          qrCode: res.data.data.qrCode
        })
        that.data.feedType =  res.data.data.qrCode.feedType
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
  },
  bindInput(res){
    console.log('bindInput',res)
    var key = res.currentTarget.dataset.key;
    if(key == 'feed_type'){
      this.data.feedType = res.detail;
    } else if(key == 'weight'){
      this.data.weight = res.detail;
    }
  },
  /**
   * 上报数据的 http请求
   */
  submitOp: function () {
    var that = this;
    fetchApi.httpPost('/device/range/add/feed/record', {
      qr_code_id:that.data.qr_id,
      feed_type: that.data.feedType,
      weight: that.data.weight,
    }, function (res) {
      console.log("上报数据的响应数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        wx.navigateBack({
          delta: -1
        })
      }else{
        Toast.fail(res.data.msg);
      }
    }, function (res) {
    })
  },
})